load('perhourttweet.mat');
load('DJIAhalfhour.mat');
numTokens=8;
numDays_train=100;
Y=[];
numDays=size(tweetdate,1);
for ii=1:numDays
    startdate=char(tweetdate(ii,:));
    findindex=0;
    for j=1:length(stockdate)
        if isequal(char(stockdate(j,:)),startdate)
            findindex=j;
        end
    end
    Y=[Y,(stocksval(findindex+1,size(stocksval,2))-stocksval(findindex,size(stocksval,2)))/stocksval(findindex,size(stocksval,2))*100];
end

x=zeros(numDays,numTokens);
for i=1:numDays
    x(i,:)=sum(perhourtweets(1:numTokens,24*(i-1)+1:24*i)');
    x(i,:)=x(i,:)/sum(x(i,:));
end
threshold=sum(x)/numDays;
for i=1:numDays
    x(i,:)=(x(i,:)>=threshold);
end
x_orig=x;
Y_orig=Y;
maxiterations=200;
test_error=0;
for iteration=1:maxiterations
    order=randperm(numDays);
    x=x_orig(order,:);
    Y=Y_orig(order);
    seqs=x(1:numDays_train,:);
    distinctseqs=[seqs(1,:)];
    count=[1];
    matched=zeros(numDays_train,1);
    matched(1)=1;
    score=Y(1);
    for i=2:size(seqs,1)
        matchseq=0;
        for j=1:size(count)
            if (sum(abs(seqs(i,:)-distinctseqs(j,:)))==0)
                matchseq=j;
                matched(i)=j;
                score(j)=score(j)+Y(i);
            end
        end
        if matchseq>0
            count(matchseq)=count(matchseq)+1;
        else
            count=[count,1];
            distinctseqs=[distinctseqs;seqs(i,:)];
            score=[score,Y(i)];
        end
    end
    
    numDays_test=numDays-numDays_train;
    output=[];
    for i=numDays_train+1:numDays
        mindist=1000;
        minIndex=[];
        score_i=0;
        for j=1:size(distinctseqs,1)
            score_i=score_i+(1/(2+sum(abs(distinctseqs(j,:)-x(i,:)))))*score(j);
            if sum(abs(distinctseqs(j,:)-x(i,:)))<mindist
                mindist=sum(abs(distinctseqs(j,:)-x(i,:)));
                minIndex=j;
            else if sum(abs(distinctseqs(j,:)-x(i,:)))==mindist
                    minIndex=[minIndex,j];
                end
            end
        end
        output=[output,(sum(score(minIndex))>0)];
%         output=[output,(score_i>0)];
        
    end
    test_error=test_error+1-sum((output==(Y(numDays_train+1:numDays)>0)))/length(output);
end
test_error=test_error/maxiterations



